﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Common;
using Entities;

namespace Dao
{
    /// <summary>
    /// Provides a way of query with MainNavigation from a SQL Server Database
    /// </summary>
    public class MainNavigationData
    {
        #region Variables private
        private IList<MainNavigation> _listMain = null;
        private MainNavigation _mainNavigation = null; 
        #endregion

        #region Methods public
        /// <summary>
        /// Get all main navigation
        /// </summary>
        /// <returns>List of main navigation</returns>
        public IList<MainNavigation> GetAll()
        {
            SqlConnection conn = new SqlConnection(SQLDBHelper.CONN_STRING);
            _listMain = new List<MainNavigation>();
            try
            {
                SqlDataReader reader = SQLDBHelper.ExecuteReader(conn, CommandType.StoredProcedure, "sp_MainNavigation_GetAll", null);
                while (reader.Read())
                {
                    _mainNavigation = new MainNavigation();
                    if (!reader.IsDBNull(0))
                        _mainNavigation.MenuItemID = reader.GetByte(0);
                    if (!reader.IsDBNull(1))
                        _mainNavigation.Tiltle = reader.GetString(1);
                    if (!reader.IsDBNull(2))
                        _mainNavigation.Link = reader.GetString(2);
                    if (!reader.IsDBNull(3))
                        _mainNavigation.Order = reader.GetByte(3);
                    if (!reader.IsDBNull(4))
                        _mainNavigation.Status = reader.GetBoolean(4);
                    _listMain.Add(_mainNavigation);
                }
            }
            catch (Exception ex)
            {
                ErrorManagement.TrackLog(ex.Message);
                throw ex;
            }
            finally
            {
                if (conn.State == ConnectionState.Open)
                    conn.Close();
            }
            return _listMain;
        }
        /// <summary>
        /// Insert or Update a main navigation
        /// If MenuItemID = 1 then Insert a main navigation
        /// If MenuItemID != 1 then Update a main navigation
        /// </summary>
        /// <param name="main">Object of MainNavigation</param>
        /// <returns>True is successful, False is unsuccessful</returns>
        public bool InsertOrUpdate(MainNavigation main)
        {
            SqlConnection conn = new SqlConnection(SQLDBHelper.CONN_STRING);
            int result = 0;
            try
            {
                SqlParameter[] parms = new SqlParameter[] { 
                            new SqlParameter("@MenuItemID", SqlDbType.TinyInt),
                            new SqlParameter("@Tiltle", SqlDbType.NVarChar, 200),
                            new SqlParameter("@Link", SqlDbType.NText),
                            new SqlParameter("@Order", SqlDbType.TinyInt),
                            new SqlParameter("@Status", SqlDbType.Bit)
                        };
                parms[0].Value = main.MenuItemID;
                parms[1].Value = main.Tiltle;
                parms[2].Value = main.Link;
                parms[3].Value = main.Order;
                parms[4].Value = main.Status;
                if (main.MenuItemID == 1)
                    result = SQLDBHelper.ExecuteNonQuery(conn, CommandType.StoredProcedure, "sp_MainNavigation_Insert", parms);
                else
                    result = SQLDBHelper.ExecuteNonQuery(conn, CommandType.StoredProcedure, "sp_MainNavigation_Update", parms);
            }
            catch (Exception ex)
            {
                ErrorManagement.TrackLog(ex.Message);
                throw ex;
            }
            finally
            {
                if (conn.State == ConnectionState.Open)
                    conn.Close();
            }
            return result > 0 ? true : false;
        }
        /// <summary>
        /// Delete a Main navigation
        /// </summary>
        /// <param name="menuItemID">ID of main navigation want to delete</param>
        /// <returns>True is successful, False is unsuccessful</returns>
        public bool DeleteByID(int menuItemID)
        {
            SqlConnection conn = new SqlConnection(SQLDBHelper.CONN_STRING);
            int reslut = 0;
            try
            {

                SqlParameter[] parm = new SqlParameter[] {
                            new SqlParameter("@MenuItemID", SqlDbType.TinyInt)};
                parm[0].Value = menuItemID;
                reslut = SQLDBHelper.ExecuteNonQuery(conn, CommandType.StoredProcedure, "sp_MainNavigation_DeleteByID", parm);
            }
            catch (Exception ex)
            {
                ErrorManagement.TrackLog(ex.Message);
                throw ex;
            }
            finally
            {
                if (conn.State == ConnectionState.Open)
                    conn.Close();
            }
            return reslut > 0 ? true : false;

        } 
        #endregion
    }
}
